﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySearch;
using Kpmg.CompanyInfo;

public partial class Pages_MyView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            string iid = Utilities.getLoginIID();
            Staff staff = StaffDAO.GetObjectByUserId(iid);
            string staffNo = staff.StaffNo;
            IList<Search> list = SearchOM.ListSearchs(staffNo);
            Session["searchs"] = list;
            rpt_condition.DataSource = list;
            rpt_condition.DataBind();
        }
    }


    protected void rpt_condition_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
        {
            Search search = e.Item.DataItem as Search;
            ((Label)e.Item.FindControl("lbl_no")).Text = search.SearchID;
            ((Label)e.Item.FindControl("lbl_Owner_Staff_No")).Text = search.OwnerStaffNo;
            ((Label)e.Item.FindControl("lbl_time")).Text = search.CreatedTime.ToString();
            ((Label)e.Item.FindControl("lbl_Created_User")).Text = search.CreatedUser;
            ((LinkButton)e.Item.FindControl("lkbtn_searchName")).Text = search.Name;
            //string str_conditions = "";
            //foreach (SearchCondition condition in search.Conditions)
            //{
            //    str_conditions += condition.Key + " " + condition.Operator.ToString() + " " + condition.Value + " ; ";
            //}
            //((Label)e.Item.FindControl("lbl_conditions")).Text = str_conditions;
        }
    }
    protected void rpt_condition_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Delete") 
        { 
            Label lbl_no =  e.Item.FindControl("lbl_no") as Label;
            string SearchID = lbl_no.Text;
            SearchOM.DeleteSearch(SearchID);
            Server.Transfer("MyView.aspx");
        }

        if (e.CommandName == "Link")
        {
            int i = e.Item.ItemIndex;
            IList<Search> searchs = (IList<Search>)Session["searchs"];
            Search search = searchs[i];
            IList<SearchCondition> SearchConditions = search.Conditions;
            Session["SearchContext"] = SearchConditions;
            string url = "View.aspx?SearchName=" + search.Name + "&OwnerStaffNo=" + search.OwnerStaffNo;
            Response.Redirect(url);
        }
    }
}